/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bpmlab.kivulifx.persistence;

import bpmlab.kivulifx.entity.Project;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author bpmlab
 */
public class PersistenceImpl implements Persistence {

    @Override
    public void save(Project project) throws Exception {
        if (project == null) {
            throw new IllegalArgumentException("The project cann't null");
        }
        JAXBContext context = JAXBContext.newInstance(Project.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(project, new File(project.getFile()));
    }

    @Override
    public Project open(File file) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Project.class);
        Unmarshaller u = context.createUnmarshaller();
        return (Project) u.unmarshal(file);
    }

}
